package com.aicheyoudao.utils;

import java.util.ArrayList;

import com.baidu.mapapi.search.core.PoiInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class HistoryDB extends SQLiteOpenHelper {
	public static final String DATABASE_NAME = "History.db";
	private static final String TABLE_NAME = "historytab";
	private static final int DATABASE_VERSION = 1;
	private SQLiteDatabase database;
	private Cursor cursor;

	public HistoryDB(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	public HistoryDB(Context context) {
		this(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String dbStr = "create table if not exists " + TABLE_NAME + "(" + "_id INTEGER PRIMARY KEY, " + "name text" + ");";
		db.execSQL(dbStr);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public void insert(String name) {
		ContentValues value = new ContentValues();
		value.put("name", name);
		database = getWritableDatabase();
		database.insert(TABLE_NAME, null, value);
		database.close();
		Log.i("info", "insert");
	}

	public void delete() {
		database = getWritableDatabase();
		String delStr = "delete from " + TABLE_NAME;
		database.execSQL(delStr);

	}

	public ArrayList<PoiInfo> queryAll() {
		ArrayList<PoiInfo> events = new ArrayList<PoiInfo>();
		database = getReadableDatabase();
		cursor = database.query(TABLE_NAME, null, null, null, null, null, null);
		while (cursor.moveToNext()) {
			PoiInfo info = new PoiInfo();
			info.name = cursor.getString(cursor.getColumnIndex("name"));
			events.add(info);
		}
		cursor.close();
		database.close();
		return events;
	}

}
